<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Image;

class UploadController extends Controller
{
    public function __construct()
    {

    }

    //上传图片
    public static function images(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            if ($request->hasFile('image')) {
                $file = $request->file('image');

                if ($file->isValid()) {
                    $name = str_random(10) . '.jpg';
                    $upload_path = public_path() . '/uploads/' . date('Y-m-d');
                    $width = $request->width ? $request->width : 100;
                    $height = $request->height ? $request->height : 100;

                    if( !file_exists($upload_path) ) {
                        mkdir($upload_path, 0777, true);
                    }
                    $file_path = $upload_path . '/' . $name;
                    $image = Image::make($file)->resize($width, $height)->save($file_path);
                    // dd($image);
                    $thumbnail_path = '/uploads/' . date('Y-m-d') . '/' . $name;
                    $data = ['status' => 0, 'msg' => '图片上传成功', 'path' => $thumbnail_path];
                } else {
                    $data = ['status' => 2, 'msg' => '图片格式不对'];
                }
            } else {
                $data = ['status' => 3, 'msg' => '图片不能为空'];
            }
        }else {
            $data = ['status' => 1, 'msg' => '图片上传失败'];
        }

        return json_encode($data);


    }
}
